using System;
using UnityEngine;
using Utils;

namespace Actions
{
    /// <summary>
    /// 刚出生的一定时间内不可选择
    /// </summary>
    public class SelectableTarget : MonoBehaviour
    {
        [SerializeField] private float unselectableTime = 1f;

        private Collider2D _collider;
        private SpriteRenderer _spriteRenderer;
        
        private Color _initialColor;
        private Timer _timer;


        private void Awake()
        {
            _timer = new Timer(unselectableTime, BecomeSelectable);
            _timer.Reset();

            _collider = GetComponent<Collider2D>();
            _collider.enabled = false;

            _spriteRenderer = GetComponentInChildren<SpriteRenderer>();
            if (_spriteRenderer is null)
            {
                return;
            }

            Color color = _spriteRenderer.color;
            _initialColor = color;
            
            color.a = 0.5f;
            _spriteRenderer.color = color;
        }

        private void Update()
        {
            _timer.Tick();
        }

        private void BecomeSelectable()
        {
            _collider.enabled = true;
            enabled = false;

            if (_spriteRenderer is not null)
            {
                _spriteRenderer.color = _initialColor;
            }
        }
    }
}
